import { Service } from "../service";
import { IWorkspace } from "../model";
export function workspaceReducer(workspace: IWorkspace) {
  return workspace;
}
export const resolvers = {
  Query: {
    workspaceById: (_: any, { id }: { id: string }) =>
      Service.getWorkspaceByIds([id])[0],
    workspaces: () => Service.listWorkspaces(),
  },
  Mutation: {
    addWorkspace: (_: any, { title }: { title: string }) => {
      try {
        const workspace = Service.addWorkspace({ title });
        Service.save();
        return {
          success: true,
          workspace,
        };
      } catch (err) {
        return { success: false, message: err.message };
      }
    },
    removeWorkspaceById: (_: any, { id }: { id: string }) => {
      const workspace = Service.removeWorkspace(id);
      Service.save();
      return {
        success: true,
        workspace,
      };
    },
    removeWorkspaceByIds: (_: any, { ids }: { ids: string[] }) => {
      const workspaces = ids.map((id) => Service.removeWorkspace(id));
      Service.save();
      return {
        success: true,
        workspaces,
      };
    },
  },
};
